﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace GalaxyDefenderChaos
{
    public class SkyBox : VisibleGameEntity
    {
        const int NUM_VERTICES = 36;
        private VertexPositionNormalTexture[] _Vertices;
        private Vector3 Size;
        private Vector3 Position;
        private List<Texture2D> TextureSides;

        public SkyBox(ContentManager content, Vector3 size, Vector3 position, string[] strTextures)
        {
            Size = size;
            Position = position;
            TextureSides = new List<Texture2D>();
            for (int i = 0; i < strTextures.Count(); i++)
            {
                TextureSides.Add(content.Load<Texture2D>(strTextures[i]));
            }
            initVertice();
        }

        void initVertice()
        {
            _Vertices = new VertexPositionNormalTexture[NUM_VERTICES];

            // Calculate the position of the vertices on the top face.
            Vector3 topLeftFront = Position + new Vector3(-1.0f, 1.0f, -1.0f) * Size;
            Vector3 topLeftBack = Position + new Vector3(-1.0f, 1.0f, 1.0f) * Size;
            Vector3 topRightFront = Position + new Vector3(1.0f, 1.0f, -1.0f) * Size;
            Vector3 topRightBack = Position + new Vector3(1.0f, 1.0f, 1.0f) * Size;

            // Calculate the position of the vertices on the bottom face.
            Vector3 btmLeftFront = Position + new Vector3(-1.0f, -1.0f, -1.0f) * Size;
            Vector3 btmLeftBack = Position + new Vector3(-1.0f, -1.0f, 1.0f) * Size;
            Vector3 btmRightFront = Position + new Vector3(1.0f, -1.0f, -1.0f) * Size;
            Vector3 btmRightBack = Position + new Vector3(1.0f, -1.0f, 1.0f) * Size;

            // Normal vectors for each face (needed for lighting / display)
            Vector3 normalFront = new Vector3(0.0f, 0.0f, 1.0f) * Size;
            Vector3 normalBack = new Vector3(0.0f, 0.0f, -1.0f) * Size;
            Vector3 normalTop = new Vector3(0.0f, 1.0f, 0.0f) * Size;
            Vector3 normalBottom = new Vector3(0.0f, -1.0f, 0.0f) * Size;
            Vector3 normalLeft = new Vector3(-1.0f, 0.0f, 0.0f) * Size;
            Vector3 normalRight = new Vector3(1.0f, 0.0f, 0.0f) * Size;

            // UV texture coordinates
            Vector2 textureTopLeft = new Vector2(1.0f, 0.0f);
            Vector2 textureTopRight = new Vector2(0.0f, 0.0f);
            Vector2 textureBottomLeft = new Vector2(1.0f, 1.0f);
            Vector2 textureBottomRight = new Vector2(0.0f, 1.0f);

            // Add the vertices for the FRONT face.
            _Vertices[0] = new VertexPositionNormalTexture(topLeftFront, normalFront, textureTopLeft);
            _Vertices[1] = new VertexPositionNormalTexture(btmLeftFront, normalFront, textureBottomLeft);
            _Vertices[2] = new VertexPositionNormalTexture(topRightFront, normalFront, textureTopRight);
            _Vertices[3] = new VertexPositionNormalTexture(btmLeftFront, normalFront, textureBottomLeft);
            _Vertices[4] = new VertexPositionNormalTexture(btmRightFront, normalFront, textureBottomRight);
            _Vertices[5] = new VertexPositionNormalTexture(topRightFront, normalFront, textureTopRight);

            // Add the vertices for the BACK face.
            _Vertices[6] = new VertexPositionNormalTexture(topLeftBack, normalBack, textureTopRight);
            _Vertices[7] = new VertexPositionNormalTexture(topRightBack, normalBack, textureTopLeft);
            _Vertices[8] = new VertexPositionNormalTexture(btmLeftBack, normalBack, textureBottomRight);
            _Vertices[9] = new VertexPositionNormalTexture(btmLeftBack, normalBack, textureBottomRight);
            _Vertices[10] = new VertexPositionNormalTexture(topRightBack, normalBack, textureTopLeft);
            _Vertices[11] = new VertexPositionNormalTexture(btmRightBack, normalBack, textureBottomLeft);

            //// Add the vertices for the TOP face.
            _Vertices[12] = new VertexPositionNormalTexture(topLeftFront, normalTop, textureBottomLeft);
            _Vertices[13] = new VertexPositionNormalTexture(topRightBack, normalTop, textureTopRight);
            _Vertices[14] = new VertexPositionNormalTexture(topLeftBack, normalTop, textureTopLeft);
            _Vertices[15] = new VertexPositionNormalTexture(topLeftFront, normalTop, textureBottomLeft);
            _Vertices[16] = new VertexPositionNormalTexture(topRightFront, normalTop, textureBottomRight);
            _Vertices[17] = new VertexPositionNormalTexture(topRightBack, normalTop, textureTopRight);

            //// Add the vertices for the BOTTOM face. 
            _Vertices[18] = new VertexPositionNormalTexture(btmLeftFront, normalBottom, textureTopLeft);
            _Vertices[19] = new VertexPositionNormalTexture(btmLeftBack, normalBottom, textureBottomLeft);
            _Vertices[20] = new VertexPositionNormalTexture(btmRightBack, normalBottom, textureBottomRight);
            _Vertices[21] = new VertexPositionNormalTexture(btmLeftFront, normalBottom, textureTopLeft);
            _Vertices[22] = new VertexPositionNormalTexture(btmRightBack, normalBottom, textureBottomRight);
            _Vertices[23] = new VertexPositionNormalTexture(btmRightFront, normalBottom, textureTopRight);

            //// Add the vertices for the LEFT face.
            _Vertices[24] = new VertexPositionNormalTexture(topLeftFront, normalLeft, textureTopRight);
            _Vertices[25] = new VertexPositionNormalTexture(btmLeftBack, normalLeft, textureBottomLeft);
            _Vertices[26] = new VertexPositionNormalTexture(btmLeftFront, normalLeft, textureBottomRight);
            _Vertices[27] = new VertexPositionNormalTexture(topLeftBack, normalLeft, textureTopLeft);
            _Vertices[28] = new VertexPositionNormalTexture(btmLeftBack, normalLeft, textureBottomLeft);
            _Vertices[29] = new VertexPositionNormalTexture(topLeftFront, normalLeft, textureTopRight);

            //// Add the vertices for the RIGHT face. 
            _Vertices[30] = new VertexPositionNormalTexture(topRightFront, normalRight, textureTopLeft);
            _Vertices[31] = new VertexPositionNormalTexture(btmRightFront, normalRight, textureBottomLeft);
            _Vertices[32] = new VertexPositionNormalTexture(btmRightBack, normalRight, textureBottomRight);
            _Vertices[33] = new VertexPositionNormalTexture(topRightBack, normalRight, textureTopRight);
            _Vertices[34] = new VertexPositionNormalTexture(topRightFront, normalRight, textureTopLeft);
            _Vertices[35] = new VertexPositionNormalTexture(btmRightBack, normalRight, textureBottomRight);

        }
        SamplerState WrapUClampV = new SamplerState
        {
            AddressU = TextureAddressMode.Wrap,
            AddressV = TextureAddressMode.Clamp,
        };
        public override void Draw(GraphicsDevice graphicsDevice, Camera camera, GameTime gameTime)
        {
            graphicsDevice.RasterizerState = RasterizerState.CullNone;
            graphicsDevice.SamplerStates[0] = WrapUClampV;
            //graphicsDevice.BlendState = BlendState.AlphaBlend;
            BasicEffect effect = new BasicEffect(graphicsDevice);


            Matrix view = camera.View;
            view.Translation = Vector3.Zero;

            Matrix projection = camera.Projection;
            projection.M13 = projection.M14;
            projection.M23 = projection.M24;
            projection.M33 = projection.M34;
            projection.M43 = projection.M44;


            effect.World = Matrix.Identity;
            effect.View = view;
            effect.Projection = projection;
            effect.TextureEnabled = true;
            for (int i = 0; i < 6; i++) {
                effect.Texture = TextureSides[i];

                //effect.FogEnabled = true;
                //effect.FogColor = new Vector3(0.15f);
                //effect.FogStart = 100;
                //effect.FogEnd = 320;

                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Apply();

                    graphicsDevice.DrawUserPrimitives
                        <VertexPositionNormalTexture>(
                        PrimitiveType.TriangleList,
                        _Vertices, 6*i, 2);
                }
            }
            
        }
    }
}
